.<template>
	<view>
		<view class="top-box">
			<view class="top-box-title-box">
				<view class="t-b-t-img">
					<u--image :src="staticFileUrl+'/image/device/facility-norm/tips-trumpet.svg'" width="50" height="50"
						mode="aspectFit" />
				</view>
				<view class="t-b-t-text">
					设备准入合法规范
				</view>
			</view>
			<view class="t-b-content">
				用户在注册设备时，不得注册违反国家相关法律法规以及平台规则的相关设备。如发现交易异常，可能被采取相关措施
			</view>
		</view>
		<view class="padding-0-30">
			<view class="u-m-t-30">
				<u--image :src="staticFileUrl+'/image/device/facility-norm/hint-1.svg'" width="100%" height="auto"
					mode="widthFix" />
			</view>
			<view class="u-m-t-20">
				<u--image :src="staticFileUrl+'/image/device/facility-norm/hint-2.svg'" width="100%" height="auto"
					mode="widthFix" />
			</view>

			<view class="bottom-check">
				<u-checkbox-group v-model="checkboxValue">
					<u-checkbox activeColor="#6072FF" shape="circle" name="1" label="我清楚了，确定接入的设备都符合相关法律法规" labelSize="13">
					</u-checkbox>
				</u-checkbox-group>
			</view>

			<view class="u-m-t-40 u-m-b-80">
				<u-button shape="circle" color="rgb(96,114,255);" type="primary" text="扫码注册设备" @click="register()">
				</u-button>
			</view>
		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				checkboxValue: [],
				staticFileUrl: this.$configPath.staticFileUrl,
			}
		},
		methods: {

			checkboxChange() {
				this.checkboxValue = !this.checkboxValue
			},

			register() {
				if (this.checkboxValue.length == 1) {
					this.qRCodeAdd();
				} else {
					this.$helpers.toast('请勾选已阅读规范');
				}
			},

			qRCodeAdd() {

				uni.showLoading({
					title: "正在调起...",
					mask: true
				})

				var that = this;
				uni.scanCode({
					success: function(res) {
						uni.hideLoading();
						let array = res.result.split("deviceNo/");

						let deviceNo = null;
						if (array.length != 2) {
							const params = that.$helpers.getUrlParams(res.result);
							if (params.d){
								deviceNo = params.d;
							} else {
								getApp().err('二维码不规范_1：' + res.result);
								return;
							}
						} else {
							// 将扫一扫的结果转化成int类型
							try {
								deviceNo = array[1];
							} catch (e) {
								// console.log("-----转化设备号捕获到异常-------" + e)
								getApp().err('转化设备号捕获到异常：' + e);
								return;
							}
						}

						if (deviceNo == null || deviceNo == '') {
							// console.log("------转化结果有误------" + deviceNo)
							getApp().err('转化结果有误：' + deviceNo);
							return;
						}
						// console.log("------转化成功------" + deviceNo)
						// 判断是否已经绑定了
						that.findIsBinding(deviceNo);
					},
					fail: function() {
						uni.hideLoading();
					}
				});
			},

			// 进来判断是否被绑定了
			findIsBinding(deviceNo) {
				uni.showLoading({
					title: "加载中...",
					mask: true
				})

				this.$api('device.findIsBinding', {
						deviceNo: deviceNo
					}).then(res => {
						uni.hideLoading();
						if (res.status == 1) {
							// console.log(res.status);
							getApp().err(res.msg);
						} else {
							if (res.data != null) {
								const code = res.data.code
								uni.showModal({
									title: '',
									content: res.data.msg,
									success(res) {
										// 自己运营商绑定了
										if (code == 'my') {
											uni.switchTab({
												url: '/pages/home/home',
												success() {
													uni.navigateTo({
														url: '/pages-device/device-manage/device-manage?deviceNo=' +
															deviceNo,
													})
												}
											})
											// 其他运营商绑定了
										} else if (code == 'other') {

										}
									}
								});
							} else {
								uni.navigateTo({
									url: '/pages-device/facility/facility?deviceNo=' + deviceNo
								});
							}
						}
					})
					.catch(() => {
						uni.hideLoading();
					});
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #FFFFFF;
	}

	.top-box {
		background: linear-gradient(rgba(96,114,255, 255) 0%, rgba(63, 161, 254, 255) 100%, rgba(63, 161, 254, 255) 100%);
		padding: 0 50rpx 30rpx;
	}

	.top-box-title-box {
		text-align: center;
		position: relative;
		padding-top: 30rpx;
	}

	.t-b-t-img {
		position: absolute;
		left: 20rpx;
		top: 0rpx;
	}

	.t-b-t-text {
		font-size: 36rpx;
		color: #FFFFFF;
	}

	.t-b-content {
		font-size: 26rpx;
		color: #FFFFFF;
		margin-top: 40rpx;
		line-height: 45rpx;
		padding: 0 20rpx;
	}

	.padding-0-30 {
		padding: 0 30rpx;
	}

	.bottom-check {
		margin-top: 40rpx;
		display: flex;
	}

	.bottom-box-text {
		font-size: 26rpx;
		color: rgba(0, 0, 0, 0.6);
	}
</style>